var func = require('../../utils/func.js');
var net = require('../../utils/ajax.js');
var url = require('../../utils/url.js');
var user = require('../../utils/user.js');

let me;
let uid;

let loadPublished = () => {
    func.getStorge("articles_" + uid.toString()).then((res) => {
        console.log(res);
        me.setData({
            publishList: res.data
        })
    }).catch(e => {
        console.log(e);
    })
};

let loadDrafts = () => {
    func.getStorge("drafts_" + uid.toString()).then((res) => {
        console.log(res);
        transformDrafts(res);
    }).catch(e => {
        console.log(e);
    })
}

let transformDrafts = (res) => {
    for (let i = 0; i < res.data.length; i++) {
        res.data[i].time = func.dateFormat(res.data[i].last_update_time, "yyyy-MM-dd hh:mm");
    }
    me.setData({
        drafts: res.data
    })
}

Page({
    data: {
        listType: "",
        drafts: [],
        publishList: []
    },
    onLoad(e) {
        me = this;

        uid = user.userInfo.uid;

        this.setData({
            listType: e.type
        })
    },
    toEdit(e) {
        if (e.target.dataset.acttype == "delete") {
            return;
        }
        let id = e.currentTarget.dataset.id;
        func.navigateTo("../articleEdit/articleEdit?id=" + id);
    },
    deleteDraft(e) {
        let id = e.currentTarget.dataset.id;
        let drafts = this.data.drafts;

        func.showModal("提示", "您确定要删除该草稿吗?").then(() => {
            console.log("确定")
            net.send(url.deleteDraft, {
                id: id
            }).then((res) => {
                console.log(res);
                if (res.status == "y") {
                    for (let i = 0; i < drafts.length; i++) {
                        if (drafts[i].id == id) {
                            drafts.splice(i, 1);
                            this.setData({
                                drafts: drafts
                            })
                            func.setStorge("drafts_" + uid.toString(), drafts);
                            return;
                        }
                    }
                }
            })
        });
    },
    deleteArticle(e) {
        let id = e.currentTarget.dataset.id;
        let publishList = this.data.publishList;

        func.showModal("提示", "您确定要删除该文章吗?").then(() => {
            console.log("确定")
            net.send(url.deleteArticle, {
                resource_id: id
            }).then((res) => {
                console.log(res);
                if (res.status == "y") {
                    for (let i = 0; i < publishList.length; i++) {
                        if (publishList[i].id == id) {
                            publishList.splice(i, 1);
                            this.setData({
                                publishList: publishList
                            })
                            func.setStorge("articles_" + uid.toString(), publishList);
                            return;
                        }
                    }
                }
            })
        });
    },
    onShow() {
        if (this.data.listType == "draft") {
            wx.setNavigationBarTitle({ title: '我的草稿' });
            loadDrafts();
        } else if (this.data.listType == "publish") {
            wx.setNavigationBarTitle({ title: '我的创作' });
            loadPublished();
        }
    },
    toPublishedPage(e) {
        if (e.target.dataset.acttype == "delete") {
            return;
        }
        let id = e.currentTarget.dataset.id;
        func.navigateTo("../publishedPage/publishedPage?id=" + id);
    }
})